using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Common;

namespace MINERP.COM.DBHelper {
	/// <summary>The ado ex cache.</summary>
	public static class AdoExCache {
		/// <summary>The cnstr cache.</summary>
		private static readonly Dictionary<string, string> cnstrCache = new Dictionary<string, string>();
		/// <summary>The db type cache.</summary>
		private static readonly Dictionary<string, string> dbTypeCache = new Dictionary<string, string>();
		/// <summary>The parm cache.</summary>
		private static readonly Dictionary<string, DbParameter[]> parmCache = new Dictionary<string, DbParameter[]>();
		/// <summary>The sql cache.</summary>
		private static readonly Dictionary<string, string> sqlCache = new Dictionary<string, string>();
		/// <summary>The cache cn str.</summary>
		/// <param name="cacheKey">The cache key.</param>
		/// <param name="cnstr">The cnstr.</param>
		public static void CacheCnStr(string cacheKey, string cnstr) {
			cnstrCache[cacheKey] = cnstr;
		}
		/// <summary>The cache db type.</summary>
		/// <param name="cacheKey">The cache key.</param>
		/// <param name="dbType">The db type.</param>
		public static void CacheDbType(string cacheKey, string dbType) {
			dbTypeCache[cacheKey] = dbType;
		}
		/// <summary>The cache parameters.</summary>
		/// <param name="cacheKey">The cache key.</param>
		/// <param name="cmdParms">The cmd parms.</param>
		public static void CacheParameters(string cacheKey, params DbParameter[] cmdParms) {
			parmCache[cacheKey] = cmdParms;
		}
		/// <summary>The cache sql.</summary>
		/// <param name="cacheKey">The cache key.</param>
		/// <param name="sql">The sql.</param>
		public static void CacheSql(string cacheKey, string sql) {
			sqlCache[cacheKey] = sql;
		}
		/// <summary>The get cached cn str.</summary>
		/// <param name="cacheKey">The cache key.</param>
		/// <returns>The get cached cn str.</returns>
		public static string GetCachedCnStr(string cacheKey) {
			if (cnstrCache.ContainsKey(cacheKey)) {
				return cnstrCache[cacheKey];
			}
			return string.Empty;
		}
		/// <summary>The get cached db type.</summary>
		/// <param name="cacheKey">The cache key.</param>
		/// <returns>The get cached db type.</returns>
		public static string GetCachedDbType(string cacheKey) {
			if (dbTypeCache.ContainsKey(cacheKey)) {
				return dbTypeCache[cacheKey];
			}
			return string.Empty;
		}
		/// <summary>The get cached parameters.</summary>
		/// <param name="cacheKey">The cache key.</param>
		/// <returns></returns>
		public static DbParameter[] GetCachedParameters(string cacheKey) {
			if (!parmCache.ContainsKey(cacheKey)) {
				return null;
			}
			DbParameter[] parameterArray = parmCache[cacheKey];
			if (parameterArray == null) {
				return null;
			}
			DbParameter[] parameterArray2 = new DbParameter[parameterArray.Length];
			int index = 0;
			int length = parameterArray.Length;
			while (index < length) {
				parameterArray2[index] = (DbParameter) ((ICloneable) parameterArray[index]).Clone();
				index++;
			}
			return parameterArray2;
		}
		/// <summary>The get cached sql.</summary>
		/// <param name="cacheKey">The cache key.</param>
		/// <returns>The get cached sql.</returns>
		public static string GetCachedSql(string cacheKey) {
			if (sqlCache.ContainsKey(cacheKey)) {
				return sqlCache[cacheKey];
			}
			return string.Empty;
		}
		/// <summary>The get cn string.</summary>
		/// <param name="index">The index.</param>
		/// <returns>The get cn string.</returns>
		public static string GetCnString(int index) {
			string cachedCnStr = GetCachedCnStr(index.ToString());
			if (string.IsNullOrEmpty(cachedCnStr)) {
				cachedCnStr = ConfigurationManager.ConnectionStrings[index].ConnectionString;
				CacheCnStr(index.ToString(), cachedCnStr);
			}
			return cachedCnStr;
		}
		/// <summary>The get cn string.</summary>
		/// <param name="name">The name.</param>
		/// <returns>The get cn string.</returns>
		public static string GetCnString(string name) {
			string cachedCnStr = GetCachedCnStr(name);
			if (string.IsNullOrEmpty(cachedCnStr)) {
				cachedCnStr = ConfigurationManager.ConnectionStrings[name].ConnectionString;
				CacheCnStr(name, cachedCnStr);
			}
			return cachedCnStr;
		}
		/// <summary>The get db type.</summary>
		/// <param name="index">The index.</param>
		/// <returns>The get db type.</returns>
		public static string GetDbType(int index) {
			string cachedDbType = GetCachedDbType(index.ToString());
			if (string.IsNullOrEmpty(cachedDbType)) {
				cachedDbType = ConfigurationManager.ConnectionStrings[index].ProviderName;
				CacheDbType(index.ToString(), cachedDbType);
			}
			return cachedDbType;
		}
		/// <summary>The get db type.</summary>
		/// <param name="name">The name.</param>
		/// <returns>The get db type.</returns>
		public static string GetDbType(string name) {
			string cachedDbType = GetCachedDbType(name);
			if (string.IsNullOrEmpty(cachedDbType)) {
				cachedDbType = ConfigurationManager.ConnectionStrings[name].ProviderName;
				CacheDbType(name, cachedDbType);
			}
			return cachedDbType;
		}
	}
}
